<template>
	<view class="container">
		<!-- 安全区域适配刘海屏 -->
		<view class="safe-area"></view>
		
		<!-- 页面标题 -->
		<view class="header">
			<view class="back-icon" @click="goBack">
				<u-icon name="arrow-left" color="#333333" size="20"></u-icon>
			</view>
			<text class="title">我的原创</text>
		</view>
		
		<!-- 标签栏 -->
		<view class="tabs">
			<view 
				v-for="(tab, index) in tabs" 
				:key="index" 
				class="tab-item" 
				:class="{ active: currentTab === index }"
				@click="switchTab(index)"
			>
				<text class="tab-text">{{ tab }}</text>
			</view>
		</view>
		
		<!-- 文章列表 -->
		<scroll-view scroll-y class="article-list">
			<view 
				v-for="(article, index) in filteredArticles" 
				:key="index" 
				class="article-item"
				@click="viewArticle(article.id)"
			>
				<view class="article-info">
					<text class="article-title">{{ article.title }}</text>
					<view class="article-meta">
						<view class="meta-item">
							<u-icon name="calendar" color="#999999" size="14"></u-icon>
							<text class="meta-text">{{ article.publishDate }}</text>
						</view>
						<view class="meta-item">
							<u-icon name="thumb-up" color="#999999" size="14"></u-icon>
							<text class="meta-text">{{ article.likeCount }}</text>
						</view>
						<view class="meta-item" v-if="article.status === 'draft'">
							<u-icon name="edit-pen" color="#FF9800" size="14"></u-icon>
							<text class="meta-text status-draft">草稿</text>
						</view>
					</view>
				</view>
				<view class="article-arrow">
					<u-icon name="arrow-right" color="#CCCCCC" size="16"></u-icon>
				</view>
			</view>
			
			<!-- 无数据提示 -->
			<view class="empty-tip" v-if="filteredArticles.length === 0">
				<u-icon name="info-circle" color="#CCCCCC" size="40"></u-icon>
				<text class="empty-text">暂无数据</text>
			</view>
		</scroll-view>
		
		<!-- 悬浮按钮 - 新建文章 -->
		<view class="float-btn" @click="createArticle">
			<u-icon name="plus" color="#FFFFFF" size="24"></u-icon>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			tabs: ['最新发布', '最多点赞', '草稿箱'],
			currentTab: 0,
			articles: [
				{
					id: 1,
					title: '行测数量关系解题技巧分享：巧用方程法',
					publishDate: '2023-12-10',
					likeCount: 156,
					status: 'published'
				},
				{
					id: 2,
					title: '我的省考备考心得：如何在三个月内提高80分',
					publishDate: '2023-11-25',
					likeCount: 238,
					status: 'published'
				},
				{
					id: 3,
					title: '申论热点素材整理：乡村振兴政策解读',
					publishDate: '2023-11-15',
					likeCount: 97,
					status: 'published'
				},
				{
					id: 4,
					title: '面试经验分享：如何应对结构化面试中的压力问题',
					publishDate: '2023-10-30',
					likeCount: 185,
					status: 'published'
				},
				{
					id: 5,
					title: '公考时政热点分析：碳达峰碳中和政策解读',
					publishDate: '',
					likeCount: 0,
					status: 'draft'
				},
				{
					id: 6,
					title: '行测言语理解解题方法总结',
					publishDate: '',
					likeCount: 0,
					status: 'draft'
				},
				{
					id: 7,
					title: '2024年国考申论热点预测',
					publishDate: '',
					likeCount: 0,
					status: 'draft'
				}
			]
		}
	},
	onLoad() {
		console.log('我的原创页面加载成功');
		// 获取系统信息，适配刘海屏
		uni.getSystemInfo({
			success: (res) => {
				console.log('获取系统信息成功', res.statusBarHeight);
				this.statusBarHeight = res.statusBarHeight || 20;
			},
			fail: (err) => {
				console.error('获取系统信息失败', err);
			}
		});
	},
	onShow() {
		console.log('我的原创页面显示');
		// 确保当前标签正确设置
		// 不重置标签，保留用户上次选择
	},
	computed: {
		filteredArticles() {
			// 根据当前标签筛选文章
			switch(this.currentTab) {
				case 0: // 最新发布
					return [...this.articles]
						.filter(article => article.status === 'published')
						.sort((a, b) => {
							// iOS兼容的日期解析
							const dateA = a.publishDate.replace(' ', 'T');
							const dateB = b.publishDate.replace(' ', 'T');
							return new Date(dateB) - new Date(dateA);
						});
				case 1: // 最多点赞
					return [...this.articles]
						.filter(article => article.status === 'published')
						.sort((a, b) => {
							return b.likeCount - a.likeCount;
						});
				case 2: // 草稿箱
					return this.articles.filter(article => article.status === 'draft');
				default:
					return this.articles;
			}
		}
	},
	methods: {
		// 切换标签
		switchTab(index) {
			this.currentTab = index;
		},
		
		// 查看文章详情
		viewArticle(id) {
			console.log(`查看文章ID: ${id}`);
			// uni.navigateTo({
			// 	url: `/pages/original/detail?id=${id}`
			// });
		},
		
		// 创建新文章
		createArticle() {
			console.log('创建新文章');
			// uni.navigateTo({
			// 	url: '/pages/original/edit'
			// });
		},
		
		// 返回上一页
		goBack() {
			console.log('返回首页');
			// 使用switchTab直接切换到首页标签
			uni.switchTab({
				url: '/pages/index/index',
				success: () => {
					console.log('成功返回首页');
				},
				fail: (err) => {
					console.error('switchTab返回首页失败:', err);
					// 如果switchTab失败，尝试使用reLaunch
					uni.reLaunch({
						url: '/pages/index/index',
						fail: (err2) => {
							console.error('reLaunch也失败:', err2);
							// 最后尝试使用navigateBack
							uni.navigateBack({
								delta: 1
							});
						}
					});
				}
			});
		}
	}
}
</script>

<style>
/* 全局容器样式 */
.container {
	display: flex;
	flex-direction: column;
	background-color: #f8f8f8;
	min-height: 100vh;
	box-sizing: border-box;
	position: relative; /* 为悬浮按钮提供定位上下文 */
}

/* 安全区域适配刘海屏 */
.safe-area {
	width: 100%;
	height: var(--status-bar-height);
	background-color: #f8f8f8;
}

/* 页面标题样式 */
.header {
	width: 100%;
	padding: 12px 15px;
	display: flex;
	align-items: center;
	background-color: #ffffff;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
	position: relative;
}

.back-icon {
	position: absolute;
	left: 15px;
}

.title {
	flex: 1;
	font-size: 18px;
	font-weight: 500;
	color: #333333;
	text-align: center;
}

/* 标签栏样式 */
.tabs {
	display: flex;
	background-color: #ffffff;
	padding: 0 5px;
	border-bottom: 1px solid #f0f0f0;
}

.tab-item {
	flex: 1;
	height: 44px;
	display: flex;
	justify-content: center;
	align-items: center;
	position: relative;
}

.tab-text {
	font-size: 15px;
	color: #666666;
}

.tab-item.active .tab-text {
	color: #5D9CEC;
	font-weight: 500;
}

.tab-item.active::after {
	content: '';
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	width: 20px;
	height: 3px;
	background-color: #5D9CEC;
	border-radius: 2px;
}

/* 文章列表样式 */
.article-list {
	flex: 1;
	padding: 10px 15px;
}

.article-item {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 15px;
	background-color: #ffffff;
	border-radius: 8px;
	margin-bottom: 10px;
	box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.article-info {
	flex: 1;
	margin-right: 10px;
}

.article-title {
	font-size: 16px;
	color: #333333;
	line-height: 1.4;
	margin-bottom: 8px;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
	overflow: hidden;
	text-overflow: ellipsis;
}

.article-meta {
	display: flex;
	align-items: center;
	flex-wrap: wrap;
}

.meta-item {
	display: flex;
	align-items: center;
	margin-right: 15px;
	margin-bottom: 4px;
}

.meta-text {
	font-size: 12px;
	color: #999999;
	margin-left: 4px;
}

.status-draft {
	color: #FF9800;
}

/* 空数据提示 */
.empty-tip {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	padding: 50px 0;
}

.empty-text {
	font-size: 14px;
	color: #999999;
	margin-top: 10px;
}

/* 悬浮按钮 */
.float-btn {
	position: fixed;
	right: 20px;
	bottom: 30px;
	width: 56px;
	height: 56px;
	background-color: #5D9CEC;
	border-radius: 28px;
	display: flex;
	justify-content: center;
	align-items: center;
	box-shadow: 0 2px 8px rgba(93, 156, 236, 0.4);
	z-index: 100;
}
</style> 